
package it.csttech.date;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

import org.glassfish.hk2.api.MultiException;
import org.glassfish.jersey.server.ParamException;

/**
 * OVERVIEW This class provides a mapper for every MultiException thrown
 * 
 * @author marcello.felappi
 * @author marco.dalena
 */
@Provider
public class MultiExceptionMapper implements ExceptionMapper<MultiException> {

	/**
	 * Method that builds the response with the correct status code and message.
	 * 
	 * @param MultiException
	 *            exception - exception used to build the response with the
	 *            correct status (500 if cause is null, 400 for other cases.
	 * @return Response response
	 */
	@Override
	public Response toResponse(MultiException exception) {

		Throwable cause = exception.getCause();
		if (cause != null && cause instanceof ParamException)
			return Response.status(400).entity(new ErrorObject(cause)).type(
				MediaType.APPLICATION_JSON).build();
		else
			return Response.status(500).entity(new ErrorObject(exception)).type(
				MediaType.APPLICATION_JSON).build();
	}

}
